package com.ewhale.feitengguest.ui.user.task;

import android.os.Bundle;
import android.os.Handler;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.ewhale.feitengguest.R;
import com.ewhale.feitengguest.dto.TaskListDto;
import com.ewhale.feitengguest.presenter.user.task.SearchTaskPresenter;
import com.ewhale.feitengguest.ui.user.task.adapter.SearchTaskAdpater;
import com.ewhale.feitengguest.view.user.task.SearchTaskView;
import com.lcodecore.tkrefreshlayout.RefreshListenerAdapter;
import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout;
import com.simga.library.activity.MBaseActivity;
import com.simga.library.utils.CheckUtil;
import com.simga.library.utils.Dp2PxUtil;
import com.simga.library.utils.KeyBoardUtils;
import com.simga.library.widget.CoustomRefreshView;
import com.simga.library.widget.RecycleViewDivider;

import java.util.ArrayList;
import java.util.List;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import butterknife.BindView;

public class SearchTaskActivity extends MBaseActivity<SearchTaskPresenter> implements SearchTaskView {

    @BindView(R.id.tv_search)
    TextView tvSearch;
    @BindView(R.id.et_content)
    EditText etContent;
    @BindView(R.id.list)
    RecyclerView mList;
    @BindView(R.id.ref_layout)
    CoustomRefreshView refLayout;
    @BindView(R.id.iv_back)
    ImageView ivBack;
    private List<TaskListDto> datas;
    private SearchTaskAdpater mAdpater;
    private int pageNum = 1;

    public static void open(MBaseActivity mContext) {
        mContext.startActivity(null, SearchTaskActivity.class);
    }

    @Override
    protected int getViewId() {
        return R.layout.activity_search_task;
    }

    @Override
    protected void init(Bundle savedInstanceState) {
        datas = new ArrayList<>();
        mList.setLayoutManager(new LinearLayoutManager(mContext));
        mList.setHasFixedSize(true);
        mList.addItemDecoration(new RecycleViewDivider(mContext,
                Dp2PxUtil.dip2px(mContext, 5)));
        mAdpater = new SearchTaskAdpater(datas);
        mList.setAdapter(mAdpater);
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                KeyBoardUtils.showSoftInputFromWindow(mContext, etContent);
            }
        }, 200);
    }

    @Override
    protected boolean isUseBaseTitleBar() {
        return false;
    }

    @Override
    protected void initListener() {
        refLayout.setOnRefreshListener(new RefreshListenerAdapter() {
            @Override
            public void onRefresh(TwinklingRefreshLayout refreshLayout) {
                super.onRefresh(refreshLayout);
                pageNum = 1;
                String content = etContent.getText().toString();
                presenter.searchTaskList(pageNum, content);
            }

            @Override
            public void onLoadMore(TwinklingRefreshLayout refreshLayout) {
                super.onLoadMore(refreshLayout);
                pageNum++;
                String content = etContent.getText().toString();
                presenter.searchTaskList(pageNum, content);
            }
        });
        tvSearch.setOnClickListener(v -> {
            String content = etContent.getText().toString();
            if (CheckUtil.isNull(content)) {
                showToast("请输入搜索关键词");
                return;
            }
            etContent.setCursorVisible(false);
            etContent.setFocusable(false);
            etContent.setFocusableInTouchMode(false);
            KeyBoardUtils.closeKeybord(mContext, etContent);
            pageNum = 1;
            presenter.searchTaskList(pageNum, content);
        });
        etContent.setOnTouchListener((v, event) -> {
            etContent.setCursorVisible(true);
            etContent.setFocusable(true);
            etContent.setFocusableInTouchMode(true);
            etContent.requestFocus();
            return false;
        });
        mAdpater.setOnItemClickListener((view, position) -> TaskDetailsActivity.open(mContext, datas.get(position).getId()));
        ivBack.setOnClickListener(v -> {
            finish();
        });
    }


    @Override
    public void showProLoading() {
        showLoading();
    }

    @Override
    public void dimissProLoading() {
        dismissLoading();
    }

    @Override
    public void showErrorMessage(int postCode, String code, String message) {
        showErrorMsg(code, message);
    }

    @Override
    public void showTaskList(List<TaskListDto> result, int pageNum) {
        if (pageNum == 1) {
            datas.clear();
        }
        datas.addAll(result);
        mAdpater.notifyDataSetChanged();
        if (datas.size() == 0) {
            showToast("抱歉，搜索不到任务");
        }
        onLoad(result.size());
    }

    public void onLoad(int size) {
        if (refLayout == null) {
            return;
        }
        refLayout.finishRefreshLoadingMore();
        if (size < 10) {
            refLayout.enableLoadMore(false);
        } else {
            refLayout.enableLoadMore(true);
        }
    }
}
